﻿package com.ufenlei.LTS{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;

	public class ImageLoader extends Sprite {
		var bitmap:Sprite = new Sprite();
		var map:Bitmap = new Bitmap();
		var mWidth:int;
		var mHeight:int;
		var ratio:Number;
		var autoRatio:Boolean;
		public function ImageLoader(url:String = null,mapWidth:int = 0,mapHeight:int = 0,ra:Boolean = false) {
			autoRatio=ra;
			mWidth=mapWidth;
			mHeight=mapHeight;
			var imageLoader:Loader = new Loader();
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadComplete);
			imageLoader.load(new URLRequest(url));
		}
		private function imageLoadComplete(event:Event):void {
			addChild(bitmap);
			map = event.target.content;
			bitmap.addChild(map);
			if (autoRatio==true) {
				ratio=bitmap.width/bitmap.height;
				if (mWidth>=mHeight*ratio) {
					this.width=mHeight*ratio;
					this.height=mHeight;
				} else {
					this.width=mHeight*ratio;
					this.height=mHeight;
				}
			} else {
				this.width=mWidth;
				this.height=mHeight;
			}
			this.addChild(bitmap);
			var completeEvent:Event=new Event("complete");
			this.dispatchEvent(completeEvent);
		}
	}
}